//
//  CJUtils.m
//  DatangLife
//
//  Created by 娜 on 2018/4/23.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJUtils.h"
#import <MJRefresh.h>

@implementation CJUtils


+ (void)beginPullRefreshForScrollView:(UIScrollView *)scrollerView{
    [scrollerView.mj_header beginRefreshing];
}

+ (BOOL)headerIsRefreshForScrollerView:(UIScrollView *)scrollerView{
    BOOL flag = scrollerView.mj_header.isRefreshing;
    return flag;
}

+ (BOOL)footerIsRefreshForScrollerView:(UIScrollView *)scrollerView{
    return scrollerView.mj_footer.isRefreshing;
}

+ (void)noticeNoMoreDataForScrollerView:(UIScrollView *)scrollerView{
    [scrollerView.mj_footer endRefreshingWithNoMoreData];
}

+ (void)resetNoMoreDataForScrollerView:(UIScrollView *)scrollerView{
    [scrollerView.mj_footer resetNoMoreData];
}

+ (void)endRefreshForScrollerView:(UIScrollView *)scrollerView{
    [scrollerView.mj_header endRefreshing];
}

+ (void)endLoadMoreForScrollerView:(UIScrollView *)scrollerView{
    [scrollerView.mj_footer endRefreshing];

}

+ (void)hiddenFooterForScrollerView:(UIScrollView *)scrollerView{
    scrollerView.mj_footer.hidden = YES;
}

+ (void)hiddenHeaderForScrollerView:(UIScrollView *)scrollerView{
    scrollerView.mj_header.hidden = YES;
}

+ (void)addLoadMoreForScrollerView:(UIScrollView *)scrollerView loadMoreCallBack:(CJRefreshAndLoadMoreHandle)loadMoreCallBlackBlock{
    if (scrollerView == nil || loadMoreCallBlackBlock == nil) {
        return;
    }
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (loadMoreCallBlackBlock) {
            loadMoreCallBlackBlock();
        }
    }];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在为您加载数据" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多了..." forState:MJRefreshStateNoMoreData];
    footer.stateLabel.textColor = kRGBColor(90, 90, 90);
    footer.stateLabel.font = kFont(13.0f);
    scrollerView.mj_footer = footer;
    footer.backgroundColor = kClearColor;
}

+ (void)addPullRefreshForScrollerView:(UIScrollView *)scrollerView pullRefreshCallBack:(CJRefreshAndLoadMoreHandle)pullRefreshCallBackBlock{
    __weak typeof (UIScrollView *)weakScrollerView = scrollerView;
    if (scrollerView == nil || pullRefreshCallBackBlock == nil) {
        return;
    }
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (pullRefreshCallBackBlock) {
            pullRefreshCallBackBlock();
        }
        if (weakScrollerView.mj_footer.hidden == NO) {
            [weakScrollerView.mj_footer resetNoMoreData];
        }
    }];
    [header setTitle:@"释放更新" forState:MJRefreshStatePulling];
    [header setTitle:@"正在更新" forState:MJRefreshStateRefreshing];
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    header.stateLabel.font = kFont(13.f);
    header.stateLabel.textColor = [UIColor colorWithHexString:kNomalTextBlackColor];
    header.lastUpdatedTimeLabel.hidden = YES;
    scrollerView.mj_header = header;
}


// 公共富文本
+ (NSAttributedString *)attStringWithString:(NSString *)string keyWord:(NSString *)keyWord {
    return [self attStringWithString:string keyWord:keyWord font:kFont(16) highlightedColor:kRedColor textColor:[UIColor colorWithHexString:kNomalTextBlackColor]];
}

+ (NSAttributedString *)attStringWithString:(NSString *)string
                                    keyWord:(NSString *)keyWord
                                       font:(UIFont *)font
                           highlightedColor:(UIColor *)highlightedcolor
                                  textColor:(UIColor *)textColor {
    return [self attStringWithString:string keyWord:keyWord font:font highlightedColor:highlightedcolor textColor:textColor lineSpace:2.0];
}

+ (NSAttributedString *)attStringWithString:(NSString *)string
                                    keyWord:(NSString *)keyWord
                                       font:(UIFont *)font
                           highlightedColor:(UIColor *)highlightedcolor
                                  textColor:(UIColor *)textColor
                                  lineSpace:(CGFloat)lineSpace {
    if (string.length) {
        
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:string];
        if (!keyWord || keyWord.length == 0) {
            NSRange allRange = NSMakeRange(0, string.length);
            [attStr addAttribute:NSFontAttributeName value:font range:allRange];
            [attStr addAttribute:NSForegroundColorAttributeName value:textColor range:allRange];
            
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineSpacing = lineSpace;
            [attStr addAttribute:NSParagraphStyleAttributeName value:style range:allRange];
            return attStr;
        }
        NSRange range = [string rangeOfString:keyWord options:NSCaseInsensitiveSearch];
        // 找到了关键字
        if (range.location != NSNotFound) {
            NSRange allRange = NSMakeRange(0, string.length);
            [attStr addAttribute:NSFontAttributeName value:font range:allRange];
            [attStr addAttribute:NSForegroundColorAttributeName value:textColor range:allRange];
            [attStr addAttribute:NSForegroundColorAttributeName value:highlightedcolor range:range];
            
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineSpacing = lineSpace;
            [attStr addAttribute:NSParagraphStyleAttributeName value:style range:allRange];
        } else {
            NSRange allRange = NSMakeRange(0, string.length);
            [attStr addAttribute:NSFontAttributeName value:font range:allRange];
            [attStr addAttribute:NSForegroundColorAttributeName value:textColor range:allRange];
            
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineSpacing = lineSpace;
            [attStr addAttribute:NSParagraphStyleAttributeName value:style range:allRange];
            return attStr;
        }
        return attStr.copy;
    }
    return [[NSAttributedString alloc] init];

}












@end
